'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  get Model() {
    return this.ctx.model.User
  }

  // 匹配查询
  get searchKey() {
    return ['isOpen', 'activityCount']
  }

  // 模糊查询
  get likeKey() {
    return ['nickname', 'mobile']
  }

  // 查询前
  async beforeIndex() {
    const { ctx } = this
    const { query } = ctx
    const { helper } = ctx
    const filter = {}

    // 10次以上
    if (query.activityCount >= 11) {
      filter.activityCount = {
        $gte: 10,
      }
    }

    // 关键字模糊查询
    if (query.keyword && !query.keyword == '') {
      const reg = new RegExp(helper.toMongoRegex(query.keyword, 'i'))
      filter.$or = [{ nickname: reg }]
    }

    // 开始时间
    if (query.registerStartDate && query.registerStartDate != '')
      filter.createdAt = {
        $gte: new Date(query.registerStartDate + ' 00:00:00'),
      }

    // 结束时间
    if (query.registerEndDate && query.registerEndDate != '')
      filter.createdAt = {
        ...filter.createdAt,
        $lte: new Date(query.registerEndDate + ' 23:59:59'),
      }

    return {
      filter,
    }
  }

  // 格式化
  async formatData(data) {
    const { ctx } = this

    delete data.password

    // data.activityCount = await ctx.model.UserActivity.count({ userId: data.id })
  }
}

module.exports = Controller
